/*
 * wccli.c
 *
 *  Created on: Mar 27, 2012
 *      Author: root
 */
#include	"unp.h"

int
main(int argc, char **argv)
{
	int					sockfd;
	struct sockaddr_in	servaddr;
	//char buf[MAXLINE];
	char	sendline[MAXLINE], recvline[MAXLINE];
	int str_len=0;
	FILE *fp;

	if (argc != 2)
		err_quit("usage: tcpcli <IPaddress>");

	sockfd = Socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(SERV_PORT);
	Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

	Connect(sockfd, (SA *) &servaddr, sizeof(servaddr));

	//str_cli(stdin, sockfd);		/* do it all */

	/*for( ; ; )
	{
		fputs("message : ",stdout);
		fgets(buf, MAXLINE, stdin);
		int d=strlen(buf);
		printf("lenth : %d\n", d);

		if(!strcmp(buf,".\n")) break;
		write(sockfd, buf, strlen(buf));



		buf[str_len]=0;
		str_len=read(sockfd, buf, MAXLINE);
		printf("message from server : %s\n", buf);
		buf[str_len]=0;

	}*/


		while (Fgets(sendline, MAXLINE, stdin) != NULL) {

			if(!strcmp(sendline,".\n"))
			{
				Writen(sockfd,sendline, strlen(sendline));
				break;
			}

			Writen(sockfd, sendline, strlen(sendline));

			if ((str_len=Readline(sockfd, recvline, MAXLINE)) == 0)
				err_quit("str_cli: server terminated prematurely");



			Fputs(recvline, stdout);
			memset(recvline,0,strlen(recvline));
			Fputs(recvline,stdout);
			memset(sendline,0,strlen(sendline));
			Fputs(sendline,stdout);

		}

    printf("close client");
	exit(0);
}


